package com.kyuketsukistudios.mikatsuki 
{
	import com.carlcalderon.arthropod.Debug;
	import com.kyuketsukistudios.mikatsuki.assetmanager.AssetManager;
	import com.kyuketsukistudios.mikatsuki.data.SingletonData;
	import com.kyuketsukistudios.mikatsuki.mainmenu.MainMenu;
	import com.kyuketsukistudios.mikatsuki.preloader.Advices;
	import com.kyuketsukistudios.mikatsuki.preloader.LogoRollIntro;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageDisplayState;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	/**
	 * ...
	 * @author RZEROSTERN
	 * 
	 * ENGINE CLASS
	 * 
	 * We call every instances of the game right here on this class.
	 * 
	 * This is my game-dev vow:
	 * 
	 * I want to say thanks for all their support to these people:
	 * 
	 * Carto, for his true friendship.
	 * Chiva, Yagarasu and T1niebl4zz for the help on this project.
	 * TijoMonster, for being my master in AS3 dark arts. Without him, I would be scum.
	 * MrGreat, for the project management teachings. WE NEVER ACCEPT THE FAILURE...EVER
	 * Finally TO all the people in mexican game-dev industry, we are the revolution of Mexico.
	 * 
	 * "Prefiero morir de pie, que vivir cien años de rodillas" - Emiliano Zapata.
	 * 
	 */
	public class Engine extends Sprite
	{
		/*Variables*/
		public static var m_stageReference:Stage;
		private var m_projectBanner:TextField = new TextField();
		private var m_data:SingletonData;
		private var m_advices:Advices;
		private var m_mainmenu:MainMenu;
		private var m_logoRollIntro:LogoRollIntro;
		
		/*Public functions*/
		public function Engine() 
		{
			m_stageReference = stage;
			m_data = SingletonData.getInstance(); //Calling the data container.
			m_data.sendToConsole(0x00ff00, "Hello World"); //My log, courtesy of ARTHROPOD.
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, fullscreenMode);
			
			m_logoRollIntro = new LogoRollIntro("kyuketsukiSonorized"); /*We call our player, beyond this point we'll load the rest of the world.*/
			
			/* I need to enable fullscreen compatibility with a keypress.
			 * From the beginning we can decide if we want windowed mode or fullscreen.
			 */
			
			//We add listeners for our custom events and rolling the preloader.
			m_logoRollIntro.addEventListener("VIDEO_FINISHED", onFinishedRoll);
			addChild(m_logoRollIntro);
		}
		
		private function onFinishedRoll(p_event:Event):void 
		{
			m_data.sendToConsole(0x00ff00, "Listener received. Logos removed.");
			removeChild(m_logoRollIntro);
			m_advices = new Advices();
			m_advices.addEventListener("ADVICES_FINISHED", removePreloader);
		}
		
		private function fullscreenMode(p_event:KeyboardEvent):void 
		{
			if (p_event.keyCode == 123)
			{
				if (stage.displayState == StageDisplayState.NORMAL)
				{
					stage.displayState = StageDisplayState.FULL_SCREEN;
				}
			}
		}
		
		private function removePreloader(e:Event):void 
		{
			m_data.sendToConsole(0xff0000, "It should remove the Advices class");
			var array:Array = m_data.loadSaveFile();
			
			for (var i:int = 0; i < array.length; i++)
			{
				trace(array[i]);
			}
			
			m_mainmenu = new MainMenu(this.loaderInfo);
			addChildAt(m_mainmenu, 0);
		}
		
		/* Private functions
		 * This function express a banner when the game is on alpha or beta mode.
		 */
		public function temporaryBanner():void
		{
			m_projectBanner.multiline = true;
			m_projectBanner.text = "Mikatsuki Project - Alpha 1 \n" +
								   "Copyright 2011 Kyuketsuki Studios - All rights reserved.\n\n" +
								   "PRESS F12 FOR USE FULLSCREEN.\n" +
								   "PRESS ESC FOR EXIT FULLSCREEN.\n" +
								   "PRESS ALT + F4 FOR SHUT DOWN THIS DEMO";
			m_projectBanner.textColor = 0xffffff;
			m_projectBanner.autoSize = "left";
			m_projectBanner.selectable = false;
			m_projectBanner.x = m_projectBanner.y = 0;
			addChild(m_projectBanner);
		}
	}
}